<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               v-model:search="search"
               v-model:page="page"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.artwork_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template #menu="scope">
        <el-button round
                   type="success"
                   icon="el-icon-finished"
                   @click.stop="handleCheck(scope.row,scope.index)"
                   v-if="userInfo.authority.includes('admin') && (scope.row.status===1)">审核
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove} from "@/api/micro/artwork";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        editBtn: false,
        menuWidth: '350',
        column: [
          {
            label: "用户id",
            prop: "memberId",
            width: 80,
            rules: [{
              required: true,
              message: "请输入用户id",
              trigger: "blur"
            }]
          },
            {
              label: "用户昵称",
              prop: "nickname",
              width: 80,
              editDisplay: false,
              addDisplay: false,
            },
          {
            label: "作品主题",
            prop: "title",
            maxlength: 200,
            showWordLimit: true,
            rules: [{
              required: true,
              message: "请输入作品主题",
              trigger: "blur"
            }]
          },
          {
            label: "作品描述",
            prop: "description",
            type: 'textarea',
            minRows: 3,
            maxRows: 5,
            maxlength: 500,
            showWordLimit: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入作品描述",
              trigger: "blur"
            }]
          },
          {
            label: "作品列表",
            prop: "images",
            type: "upload",
            multiple: true,
            limit: 9,
            action: '/api/blade-resource/oss/endpoint/put-file',
            listType: 'picture-card',
            accept: 'image/png, image/jpeg',
            tip: '只能上传9张jpg/png图片',
            span: 24,
            row: true,
            propsHttp: {
              res: 'data',
              url: 'link',
            },
            width: 400,
            rules: [{
              required: true,
              message: "请输入作品的图片URL列表",
              trigger: "blur"
            }]
          },
          {
            label: "点赞数量",
            prop: "likesCount",
            type: "number",
            width: 60,
            // rules: [{
            //   //required: true,
            //   message: "请输入点赞数量",
            //   trigger: "blur"
            // }]
          },
          {
            label: "分享数量",
            prop: "sharesCount",
            type: "number",
            width: 60,
            // rules: [{
            //   required: true,
            //   message: "请输入分享数量",
            //   trigger: "blur"
            // }]
          },
          {
            label: "审核状态",
            prop: "status",
            type: "radio",
            dataType: "number",
            span: 30,
            props: {
              label: "label",
              value: "value"
            },
            dicData: [
              {
                label: "未审核",
                value: 1
              },
              {
                label: "通过",
                value: 2
              },
              {
                label: "不通过",
                value: 3
              }
            ],
            // rules: [{
            //   //required: true,
            //   message: "审核状态",
            //   trigger: "blur"
            // }]
          },
          {
            label: "审核说明",
            prop: "audit",
            maxlength: 200,
            showWordLimit: true,
            width: 60,
            span: 100,
            tip: '审核不通过，说明原因',
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.artwork_add, false),
        viewBtn: this.validData(this.permission.artwork_view, false),
        delBtn: this.validData(this.permission.artwork_delete, false),
        editBtn: this.validData(this.permission.artwork_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleCheck(row, index) {
      //审核逻辑
      const propsName = ['status', 'audit']
      this.$refs.crud.option.column.filter(item => {
        if (!propsName.includes(item.prop)) {
          item.editDisabled = true;
        }
      });
      this.$refs.crud.rowEdit(row, index);
    }
  }
};
</script>

<style>
</style>
